package com.ourteam.product.model.api;

import org.apache.commons.lang3.StringUtils;

import com.ourteam.frame.web.AbstractSearchModel;
import com.ourteam.product.model.dao.IProductModelPackageDAO;
import com.ourteam.product.model.dao.ProductModelPackageDAOQueryBean;
import com.ourteam.product.model.domain.ProductModelPackageBean;

public class ProductModelPackageSearchModel extends AbstractSearchModel<ProductModelPackageDAOQueryBean> {

	private Long domainId;

	private String packageName;

	private String packageType;

	private String excludePackageType;

	private String excludePackageIds;

	@Override
	public String getMappedDAOField(String aProperty) {
		return ProductModelPackageBean.getMappedDAOField(aProperty);
	}

	@Override
	protected void doBuildQueryBean(ProductModelPackageDAOQueryBean queryBean) throws Exception {

		ProductModelPackageDAOQueryBean.Criteria criteria = queryBean.createCriteria();

		if (domainId != null && domainId != 0) {
			criteria.andDomainIdEqualTo(domainId);
		}

		if (StringUtils.isNotBlank(packageName)) {
			criteria.andPackageNameLike(packageName);
		}

		if (StringUtils.isNotBlank(packageType)) {
			String[] packages = StringUtils.split(packageType, ",");
			criteria.andPackageTypeIn(packages);
		}

		if (StringUtils.isNotBlank(excludePackageType)) {
			if (StringUtils.contains(excludePackageType, ",")) {
				criteria.andPackageTypeNotIn(StringUtils.split(excludePackageType, ","));
			} else {
				criteria.andPackageTypeNotEqualTo(excludePackageType);
			}
		}

		if (StringUtils.isNotBlank(excludePackageIds)) {
			String[] idArray = StringUtils.split(excludePackageIds, ",");
			long[] ids = new long[idArray.length];

			for (int i = 0; i < ids.length; i++) {
				ids[i] = Long.parseLong(idArray[i]);
			}

			criteria.andIdNotIn(ids);
		}

		queryBean.setSortProperty(IProductModelPackageDAO.PackageType);
	}

	public String getPackageName() {
		return packageName;
	}

	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}

	public String getPackageType() {
		return packageType;
	}

	public void setPackageType(String packageType) {
		this.packageType = packageType;
	}

	public Long getDomainId() {
		return domainId;
	}

	public void setDomainId(Long domainId) {
		this.domainId = domainId;
	}

	public String getExcludePackageIds() {
		return excludePackageIds;
	}

	public void setExcludePackageIds(String excludePackageIds) {
		this.excludePackageIds = excludePackageIds;
	}

	public String getExcludePackageType() {
		return excludePackageType;
	}

	public void setExcludePackageType(String excludePackageType) {
		this.excludePackageType = excludePackageType;
	}

}
